package com.zeno.chinesecalendar.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.text.MessageFormat;
/**
 * @author zeno.w
 * @date 2021/11/27 16:26
 */
class MonthSheet {

    private final YearWorkBook ywb;
    private final Sheet monthSheet;

    MonthSheet(YearWorkBook ywb,int year,int month){
        this.ywb = ywb;

        this.monthSheet = ywb.createSheet(month + "月");
        monthSheet.setDefaultColumnWidth(13);
        monthSheet.setColumnWidth(0,5000);
        monthSheet.setDefaultRowHeight((short)500);

        initTitleRow(MessageFormat.format("  公历 {0}年 {1}月",String.valueOf(year),month));
    }

    public void initTitleRow(String title){
        Row row = monthSheet.createRow(0);
        row.setHeight((short)600);

        Cell titleCell = row.createCell(0);
        titleCell.setCellValue(title);
        titleCell.setCellStyle(ywb.titleCellStyle());

        CellRangeAddress cellRange = new CellRangeAddress(0,0,0,12);
        monthSheet.addMergedRegion(cellRange);
    }

    public Row addDayRow(int index){
        Row row = monthSheet.createRow(index);
        row.setHeight((short)570);
        CellRangeAddress cellRange = new CellRangeAddress(index,index+1,0,0);
        monthSheet.addMergedRegion(cellRange);
        return row;
    }

    public Row addEightTrigramRow(int index){
        Row row = monthSheet.createRow(index);
        row.setHeight((short)570);
        return row;
    }

    public void addHourCell(Row dayRow, int index, String value){
        Cell hourCell = dayRow.createCell(index);
        hourCell.setCellStyle(ywb.hourCellStyle());
        hourCell.setCellValue(value);
    }

    public void addDayCell(Row dayRow,  String value){
        Cell dayCell = dayRow.createCell(0);
        dayCell.setCellStyle(ywb.dayCellStyle());
        dayCell.setCellValue(value);
    }
}
